<route lang="json5" type="page">
{
  layout: 'main',
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '评测问卷选择',
  },
}
</route>

<template>
  <view>
    <view v-for="(item, index) in surveys" :key="index" @click="handleClick(index)">
      <SurveyItem :questionnaire="item" />
    </view>
    <wd-toast />
  </view>
</template>

<script lang="ts" setup>
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { useToast } from 'wot-design-uni'
import { getSurveyList } from '@/api/survey'
import cache from '@/utils/cache'

interface Questionnaire {
  name: string
  description: string
  type: string
}

const toast = useToast()
const surveys = ref<Questionnaire[]>([])

/* eslint-disable n/handle-callback-err */
onShow(() => {
  getSurveyList()
    .then((res) => {
      surveys.value = res.data as any[]
    })
    .catch((err) => {
      toast.error('获取问卷列表失败' + err)
    })
})
/* eslint-enable n/handle-callback-err */

const handleClick = (index: any) => {
  cache.set('survey', surveys.value[index])
  uni.navigateTo({
    url: '/pages/normal/survey',
  })
}
</script>

<style lang="scss" scoped></style>
